﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq
Imports Exp.Model
Imports Exp.DATA_Service

Public Class GetDataUtil

    Public Shared Function getMD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New System.Security.Cryptography.MD5CryptoServiceProvider()
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)

        bytesToHash = md5Obj.ComputeHash(bytesToHash)

        Dim strResult As String = ""
        Dim b As Byte

        For Each b In bytesToHash
            strResult += b.ToString("x2")
        Next

        Return strResult
    End Function
    Public Shared Function checkIsMemberActive(ByVal objUser As REM_User) As Boolean
        Dim result As Boolean = False

        If Not objUser.JoinDate Is Nothing Then
            If Not objUser.ExpireDate Is Nothing Then
                If Now.Date <= objUser.ExpireDate Then
                    result = True
                End If
            End If

        End If
        Return result
    End Function

    Public Shared Function checkIsMemberActive(ByVal User_id As Integer) As Boolean
        Dim result As Boolean = False

        Dim objUSerSV As New User_Service
        Dim objUser = objUSerSV.GetByID(User_id)

        If Not objUser.JoinDate Is Nothing Then
            If Not objUser.ExpireDate Is Nothing Then
                If Now.Date <= objUser.ExpireDate Then
                    result = True
                End If
            End If

        End If
        Return result
    End Function

    Protected Shared Sub checkPrivilege(ByRef objUser As REM_User)
        'Dim objUSV As New Exp.DATA_Service.User_Service
        'If Not objUSV.isHasRight(objUser.User_ID, MENUNAME) Then
        '    HttpContext.Current.Session.Item("USER") = Nothing
        '    HttpContext.Current.Session.Clear()
        '    Response.Redirect("login.aspx")
        'End If
    End Sub
    Public Shared Function CDateToDB(ByVal sDate As String) As Date

        If sDate.Trim <> "0" Then
            Dim iYear As Integer = CType(Right(sDate, 4), Integer)
            Dim iMonth As Integer = CType(sDate.Substring(3, 2), Integer)
            Dim iDay As Integer = CType(sDate.Substring(0, 2), Integer)
            If iYear > 2500 Then
                iYear = iYear - 543
            End If
            Dim dtReturn As New Date(iYear, iMonth, iDay)
            Return Format(dtReturn, "yyyy/MM/dd")
        End If
    End Function
    Public Shared Sub CDateFormat(ByRef sDate As String)
        If sDate.Trim <> "0" Then
            Dim iYear As Integer = CType(Right(sDate, 4), Integer)
            Dim sMonth As String = sDate.Substring(3, 2)

            If iYear > 2500 Then
                iYear = iYear - 543
            End If
            If sMonth.Length = 2 Then
                Select Case sMonth
                    Case "01"
                        sMonth = "Jan"
                    Case "02"
                        sMonth = "Feb"
                    Case "03"
                        sMonth = "Mar"
                    Case "04"
                        sMonth = "Apr"
                    Case "05"
                        sMonth = "May"
                    Case "06"
                        sMonth = "Jun"
                    Case "07"
                        sMonth = "Jul"
                    Case "08"
                        sMonth = "Aug"
                    Case "09"
                        sMonth = "Sep"
                    Case "10"
                        sMonth = "Oct"
                    Case "11"
                        sMonth = "Nov"
                    Case "12"
                        sMonth = "Dem"
                End Select
            End If
            sDate = sDate.Substring(0, 2) & " " & sMonth & " " & CType(iYear, String)
        End If
    End Sub

    Public Shared Function GetNullableInteger(ByVal p As String) As Integer?
        Try
            Return Convert.ToInt32(p)
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Public Shared Function GetNullableString(ByVal p As String) As String
        If p <> "" Then
            Return p
        End If
        Return Nothing
    End Function

    Public Shared Function GetNullableDateTime(ByVal p As String) As DateTime?
        Dim dt As Date
        If Date.TryParse(p, dt) Then
            Return dt
        Else
            Return Nothing
        End If
    End Function

End Class
